package abc.com.abc.activity;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import abc.com.abc.R;
import abc.com.abc.utils.AnalysisiUtils;
import abc.com.abc.view.CourseView;
import abc.com.abc.view.MyInfoView;
import abc.com.abc.view.VideoView1;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    /*
   中间内容栏目
     */
    private FrameLayout mBodyLayout;
    /*
    底部按钮栏目
     */
    private LinearLayout mBottonLayout;

    /*
    底部按钮
     */
    private  View mDrivingbtn,mExercisesbtn,mMyInfobtn,mMessagebtn;
    private  TextView tv_Driving,tv_Exercises,tv_MyInfo,tv_Message;
    private TextView tv_back,tv_main_title;
    private RelativeLayout rl_title_bar;
    private MyInfoView mMyInfoView;
    private VideoView1 mView;
    private CourseView mCourseView;
    private MessageActivity messageActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initButtonBar();
        setListener();
        setInitStatus();

    }
    /*
   *获取UI控件
   * */
    private void init() {
    tv_back = findViewById(R.id.tv_back);
    tv_main_title = findViewById(R.id.tv_main_title);
    tv_main_title.setText("驾考课堂");
    rl_title_bar =findViewById(R.id.title_Bar);
    rl_title_bar.setBackgroundColor(Color.parseColor("#30b4FF"));
    tv_back.setVisibility(View.GONE);
    initBodyLayout();
    }

    private void initBodyLayout() {
        mBodyLayout = (FrameLayout) findViewById(R.id.main_body);
    }

    /**
     * 获取底部导航栏目的控件
     * */

    private void initButtonBar() {
        mBottonLayout = findViewById(R.id.main_botton_bar);
        mDrivingbtn = findViewById(R.id.botton_bar_Driving_btn);
        mExercisesbtn =findViewById(R.id.botton_bar_exercises_btn);
        mMyInfobtn = findViewById(R.id.botton_bar_MyInfo_btn);
        mMessagebtn = findViewById(R.id.botton_bar_Message_btn);
        tv_Driving = (TextView)findViewById(R.id.botton_bar_text_Driving);
        tv_Exercises = (TextView)findViewById(R.id.botton_bar_text_exercises);
        tv_MyInfo = (TextView)findViewById(R.id.botton_bar_text_MyInfo);
        tv_Message = (TextView)findViewById(R.id.botton_bar_text_Message);




    };

    /**
     * 控件的点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.botton_bar_Driving_btn:
                clearBottonImageState();/*清除底部按钮的选中状态*/
                selectDisplayView(0);/**显示对应的页面*/
                break;
            case R.id.botton_bar_exercises_btn:
                clearBottonImageState();
                selectDisplayView(1);
                break;
            case R.id.botton_bar_MyInfo_btn:
                clearBottonImageState();
                selectDisplayView(2);
                break;
            case R.id.botton_bar_Message_btn:
                if (readLoginStatus()){
                    //跳转到msg页面
                    clearBottonImageState();
                    selectDisplayView(3);

                }else {
                    Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_SHORT).show();

                }

                break;
                default:
                    break;

        }

    }
    //修改登录成功时候的代码
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data !=null){
            boolean isLogin = data.getBooleanExtra("isLogin",false);
            if (isLogin){
                clearBottonImageState();
                selectDisplayView(0);
                if (mMyInfoView != null){
                    mMyInfoView.setLoginParams(isLogin);
                }
            }
        }
    }

    /**显示对应的页面*/
    private void selectDisplayView(int index) {

        removeAllView();/*移除不需要的视图*/
        createView(index);/*选择视图*/
        setSelectedStatus(index);/*设置底部按钮的选中状态*/

    }
    /*移除不需要的视图*/
    private void removeAllView() {
        for (int i = 0; i<mBodyLayout.getChildCount();i++){
            mBodyLayout.getChildAt(i).setVisibility(View.GONE);
        }

    }

    /*选择视图*/
    private void createView(int index) {
        switch (index){
            case 0://Dring页面
                if (mCourseView == null){
                    mCourseView = new CourseView(this);
                    mBodyLayout.addView(mCourseView.getmCourseView());
                }else {
                    mCourseView.getmCourseView();
                }
                mCourseView.showView();
                break;
            case 1://习题页面
                if (mView == null){
                    mView = new VideoView1(this);
                    mBodyLayout.addView(mView.getView());
                }else {
                    mView.getView();
                }
                mView.showwView();
                break;
            case 2://我的页面
                if (mMyInfoView == null){
                    mMyInfoView = new MyInfoView(this);
                    mBodyLayout.addView(mMyInfoView.getView());
                }else {
                    mMyInfoView.getView();
                }
                mMyInfoView.showView();
                break;
            case 3://text页面
                if (messageActivity == null){
                    messageActivity = new MessageActivity(this);
                    mBodyLayout.addView(messageActivity.getView());
                }else {
                    messageActivity.getView();
                }
                messageActivity.showView();
                break;

        }


    }

    /*设置底部按钮的选中状态*/
    private void setSelectedStatus(int index) {
        switch (index){
            case 0://Dring页面
                mDrivingbtn.setSelected(true);
                tv_Driving.setTextColor(Color.parseColor("#0097f7"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_Driving.setTextSize(18);
                tv_main_title.setText("我的驾校");
                break;
            case 1://习题页面
                mExercisesbtn.setSelected(true);
                tv_Exercises.setTextColor(Color.parseColor("#0097f7"));
                tv_Exercises.setTextSize(18);
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("驾考知识");
                break;
            case 2://我的页面
                mMyInfobtn.setSelected(true);
                tv_MyInfo.setTextColor(Color.parseColor("#0097f7"));
                tv_MyInfo.setTextSize(18);
                rl_title_bar.setVisibility(View.GONE);

                break;
            case 3://text页面
                mMessagebtn.setSelected(true);
                tv_Message.setTextColor(Color.parseColor("#0097f7"));
                tv_Message.setTextSize(18);
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("留言");
                break;

        }
    }



    /*清除底部按钮的选中状态*/
    private void clearBottonImageState() {
        tv_Driving.setTextColor(Color.parseColor("#666666"));
        tv_Exercises.setTextColor(Color.parseColor("#666666"));
        tv_MyInfo.setTextColor(Color.parseColor("#666666"));
        tv_Message.setTextColor(Color.parseColor("#666666"));
        tv_Driving.setTextSize(14);
        tv_Exercises.setTextSize(14);
        tv_MyInfo.setTextSize(14);
        tv_Message.setTextSize(14);
        for (int i = 0 ; i<mBottonLayout.getChildCount();i++){
            mBottonLayout.getChildAt(i).setSelected(false);
        }
    }

    /**
     * 获取底部三个按钮的点击监听事件
     * */
    private void setListener() {
        for (int i = 0; i< mBottonLayout.getChildCount(); i++){
            mBottonLayout.getChildAt(i).setOnClickListener(this);
        }
        }
    /**
     * 设置界面View初始化界面
     * */
    private void setInitStatus() {
        clearBottonImageState();

        setSelectedStatus(0);
        createView(0);

    }



//记录第一次点击的时间
protected long exitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() ==KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime)>2000){
                Toast.makeText(MainActivity.this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
                exitTime  = System.currentTimeMillis();
            }else{
                if (readLoginStatus()){
                    Log.i(" clearLoginStatus"," clearLoginStatus="+ clearLoginStatus());
                    clearLoginStatus();
                }
                android.os.Process.killProcess(android.os.Process.myPid());
            }
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    //获取sharedpreferences中的登陆状态
    private boolean readLoginStatus() {
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin",false);
        return isLogin;
    }
    //清除sharedpreferences中的登陆状态
    private boolean clearLoginStatus() {
        SharedPreferences sp = getSharedPreferences("login",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        editor.putBoolean("isLogin",false);//清除登陆状态
        editor.putString("loginUserName","");//清除登陆时候的用户名
        editor.commit();//提交修改
        boolean isLogin = sp.getBoolean("isLogin",false);
        return  isLogin;
    }




}
